<template>
  <div style="width: 180px;float: right;color: white;position: fixed;top: 20px;right: 0;z-index: 2;">
    <Select />
  </div>
  <div class="bg">
    <el-scrollbar height="612px">
      <div class="form">

        <div class="logo">
          <img :src="logo" alt="">
        </div>
        <div class="box1">
          <div class="inner inner1">
            <el-input v-model="name" :placeholder="$t('姓名')" />
          </div>
          <div class="inner inner2">
            <div style="display: flex;">
              <el-input v-model="form.email" :placeholder="$t('电子邮箱')" style=" margin-right: 10px;" />
              <div style="
                  width: 150px;
                  height: 32px;
                  border-radius: 5px;
                  font-size: 12px;
                  background-color: #f6d39c;
                  display: flex;
                  justify-content: center;
                  align-items: center;
                  cursor: pointer;
                  border:none;
                  
                " class="bt" @click="startCountdown">
                <span v-if="countdown > 0">{{ countdown }} {{ $t('秒') }} </span>
                <span v-else>{{ $t('发送验证码') }} </span>
              </div>
            </div>

          </div>
          <div class="inner inner3">
            <el-input v-model="form.code" :placeholder="$t('邮箱验证码')" />
          </div>
          <div class="inner inner4">
            <div style="display: flex;">
              <el-input v-model="inputCode" style="width: 210px; margin-right: 10px;" :placeholder="$t('验证码')" />
              <div :class="'code'" @click="createCode">{{ code }}</div>
            </div>
          </div>
          <div class="inner inner5">
            <div style="display: flex;">
              <vue3-country-intl v-model="form.phone_ex" style="border-radius: 5px;" type="phone"
                onlyValue="true"></vue3-country-intl>
              <el-input v-model="form.phone" :placeholder="$t('手机')" style="width: 210px; margin-left: 10px;" />
            </div>
          </div>
          <div class="inner inner7">
            <el-input v-model="form.password" type="password" :placeholder="$t('密码')" show-password />
          </div>
          <div class="inner inner8">
            <el-input v-model="password" type="password" :placeholder="$t('确认密码')" show-password />
          </div>
          <div class="inner inner9">
            <el-input v-model="form.security_password" :placeholder="$t('交易密码')" />
          </div>
          <div class="inner inner10">
            <el-input v-model="form.invite_code" :placeholder="$t('邀请码')" />
          </div>
          <div class="inner inner11">
            <div style="
          width: 100%;
          height: 45px;
          border-radius: 5px;
          background-color: #f6d39c;
          display: flex;
          justify-content: center;
          align-items: center;
          margin-right: 10px;
          cursor: pointer;
          border:none;
        " class="bt" @click="register">
              <span> {{ $t('立即注册') }} </span>
            </div>
          </div>
          <router-link to="/login" class="inner inner12"
            style="display: flex;justify-content: center;align-items: center;">
            <span style="color: #fff;"> {{ $t('已有账号？请登录') }} </span>
          </router-link>
          <div class="inner inner13">
            <span style="color: #fff; font-size: 13px;"> {{ $t('我们会保护您的隐私，所有信息不会透露给任何第三方!') }} </span>
          </div>
        </div>
      </div>
    </el-scrollbar>
    <div class="register">
      <span style="color: #f6d39c;"> {{ $t('邮箱注册') }} </span>
    </div>
  </div>
</template>

<script>
import { ElNotification } from 'element-plus'
import Select from '@/components/select.vue'
import router from '@/router'
import {
  sendEmailApi,
  userRegisterApi,
  settingApi
} from '@/api'
export default {
  components: {
    Select
  },
  name: 'LoginPage',
  data() {
    return {
      countdown:0,
      logo:'',
      form: {
        email: '',
        code: '',
        password: '',
        phone_ex: '86',
        phone: '',
        security_password: '',
        invite_code: ''
      },
      name: '',
      password: '',
      code: '',
      inputCode: '',
      lang: {
        show: false,
        active: {
          icon: require('../../assets/headerNav/zhong.png'),
          name: this.$t('中国'),
          value: 'zh-cn'
        },
        list: [
          {
            icon: require('../../assets/headerNav/yindu.png'),
            name: this.$t('印度语'),
            value: 'Indonesian'
          },
          {
            icon: require('../../assets/headerNav/english.png'),
            name: this.$t('英语'),
            value: 'en'
          },
          {
            icon: require('../../assets/headerNav/zhong.png'),
            name:  this.$t('简体中文'),
            value: 'zh-cn'
          },
          {
            icon: require('../../assets/headerNav/jpan.png'),
            name: this.$t('日语'),
            value: 'jpan'
          },

        ]
      },
      value: '',
      options: [
        {
          value: this.$t('邮箱登录'),
          label: this.$t('邮箱登录'),
        },
        {
          value: this.$t('账户登录'),
          label: this.$t('账户登录'),
        },
      ]
    }
  },
  methods: {
    // 60s倒计时
    startCountdown() {
      if (this.countdown === 0) {
        this.getEmailCode();
        this.countdown = 60;
        const timer = setInterval(() => {
          if (this.countdown > 0) {
            this.countdown--;
          } else {
            clearInterval(timer);
          }
        }, 1000);
      }
    },
    createCode() {
      var code = '';
      var codeLength = 4;
      var codeChars = '0123456789';
      for (var i = 0; i < codeLength; i++) {
        var charNum = Math.floor(Math.random() * codeChars.length);
        code += codeChars.charAt(charNum);
      }
      this.code = code;
    },
    userLogout() {
      /* removeUserInfo()
      this.$router.go(0) */
      this.$store.commit("user/USER_LOGOUT");
    },
    langClick(item) {
      this.lang.active = item;
      this.lang.show = false;
    },

    /* 获取配置 */
    getSetting(){
      settingApi().then(res=>{
         this.logo=res.data.logo
      })
    },
    // 发送验证码
    getEmailCode() {
      const email = this.form.email
      sendEmailApi({
        email,
        event: 'reg',
      }).then(res => {
        ElNotification({
          message: res.msg,
        });
        this.form.code=res.data.code
        console.log(res.data.code)
      })
    },
    // 注册
    register() {
      if (!this.inputCode) {
        ElNotification({
          title: 'Error',
          message: this.$t('请输入验证码'),
          type: 'error',
        });
        return;
      }
      if (this.inputCode != this.code) {
        ElNotification({
          title: 'Error',
          message:this.$t('验证码错误'),
          type: 'error',
        });
        return;
      }
      if (this.form.password != this.password) {
        ElNotification({
          title: 'Error',
          message:this.$t( '请确保确认密码正确'),
          type: 'error',
        });
        return;
      }
      userRegisterApi(this.form).then(res => {
        // 提示注册
        ElNotification({
          message: res.msg,
          duration: 1000,
          onClose: () => {
            if (res.code == 1) {
              // 跳转到/dashboard页面
              router.push('/login');
            }
          }
        })
      })
    }
  },
  mounted() {
    this.createCode()
    this.getSetting()
  }
}
</script>

<style lang="scss" scoped>
.bg {
  position: relative;
  width: 100%;
  height: 100vh;
  background-image: url(../../assets/imgs/bg.jpg);
  background-size: 100% 108%;
  display: flex;
  justify-content: center;
  align-items: center;
  padding-top: 150px;
  box-sizing: border-box;

  .register {
    position: absolute;
    width: 168px;
    height: 50px;
    background-color: #282b37;
    top: 80px;
    left: calc((100% - 350px)/2);
    display: flex;
    justify-content: center;
    align-items: center;
    border-radius: 8px;
  }

  .form {
    padding: 24px;
    box-sizing: border-box;
    background-color: #282b37;
    height: 100%;
    width: 350px;
    position: relative;
    border-radius: 15px;

    .logo {
      margin-bottom: 20px;

      img {
        width: 302px;
        height: 72px;
      }
    }

    .box1 {
      .code {
        font-family: Arial;
        font-style: italic;
        color: blue;
        font-size: 20px;
        border: 0;
        padding: 2px 0;
        font-weight: bolder;
        cursor: pointer;
        width: 80px;
        height: 30px;
        line-height: 30px;
        text-align: center;
        background-color: #ffffff;
      }



      .inner {
        margin-bottom: 15px;
      }

      .inner2 {
        display: flex;
        flex-wrap: wrap;
      }

      .inner5 {
        :deep(.vue-country-intl-inputer) {
          background-color: transparent;
        }

        // :deep(.vue-country-intl-inputer .country-intl-label){
        //   width: 120px;
        // }
      }

      .inner6 {
        display: flex;
        justify-content: space-between;
        align-items: center;
      }

      .inner7 {
        display: flex;
        justify-content: center;
        align-items: center;
      }

      .inner9 {

        .avatar-uploader .el-upload {
          border: 1px dashed var(--el-border-color);
          border-radius: 6px;
          cursor: pointer;
          position: relative;
          overflow: hidden;
          transition: var(--el-transition-duration-fast);
        }

        .avatar-uploader .el-upload:hover {
          border-color: var(--el-color-primary);
        }

        .el-icon.avatar-uploader-icon {
          font-size: 28px;
          color: #8c939d;
          width: 178px;
          height: 178px;
          text-align: center;
        }
      }

      .inner10 {
        .avatar-uploader .avatar {
          width: 178px;
          height: 178px;
          display: block;
        }
      }

      a {
        text-decoration: none;
      }
    }
  }
}
</style>
